/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: worker_discovery_proxy.cpp
 */

#include <boost/asio.hpp>
#include <exception>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>

#include "worker_discovery_proxy.hpp"

using namespace middleware;

//-----------------------------------------------------------------------------
// Constructors and destructor
/* Private constructor */
WorkerDiscoveryProxy::WorkerDiscoveryProxy() {
	this->_config.ip_to_disc = "127.0.0.1";
	this->_config.port_to_disc = "0";
	this->_config.ptrto_worker = 0;
}
/* Private paranetric constructor */
WorkerDiscoveryProxy::WorkerDiscoveryProxy(WorkerDiscProxyConfig conf) {
	this->_config.ip_to_disc = conf.ip_to_disc;
	this->_config.port_to_disc = conf.port_to_disc;
	this->_config.ptrto_worker = conf.ptrto_worker;
}
/* Destructor */
WorkerDiscoveryProxy::~WorkerDiscoveryProxy() {

}
//-----------------------------------------------------------------------------
// Public members
/* Getting worker */
Address WorkerDiscoveryProxy::get_worker(const TaskDescriptor& taskdesc)
		throw(ProxyException) {
	try {
		if (this->_config.ptrto_worker == 0)
			throw ProxyException("Null pointer to Discovery!");
		WorkerDescriptor wdesc =
				this->_config.ptrto_worker->worker_getter(taskdesc);
		return wdesc.get_address();
	} catch(std::exception& e) {
		throw ProxyException(std::string("Error in proxy: ") + e.what());
	}
}
/* Updating worker */
void WorkerDiscoveryProxy::update_worker(const WorkerDescriptor& workerdesc)
		throw(ProxyException) {
	using boost::asio::ip::tcp;
	// Managing destination unavailable
	for (int i = 10; true; i--) {
		try {
			// Sending a worker descriptor
			tcp::iostream wdesc_stream(this->_config.ip_to_disc,
					this->_config.port_to_disc); /* Creating TCP stream */
			if (!wdesc_stream) {
				// Problem in sending...
				throw ProxyException("Unable to open TCP stream!");
			} else {
				// OK we can send it
				boost::archive::binary_oarchive oa(wdesc_stream);
				oa << workerdesc; /* Sending worker descriptor --> SENT */
				wdesc_stream.close();
				return; /* Get away */
			}
		} catch(ProxyException& e) {
			// Could not reach destination: try again
			if (i <= 0) throw ProxyException(std::string(
					"Error in proxy: Unreachable destination: ") + e.what());
			boost::this_thread::sleep(boost::posix_time::milliseconds(100));
		} catch(std::exception& e) {
			// Unknown exception
			throw ProxyException(
					std::string("Not reversible error in proxy: ") + e.what());
		}
	}
}
